package com.coocaa.ops.plus.core.third.magic.handler;

import com.coocaa.ops.common.tool.base.InstanceUtil;
import com.coocaa.ops.plus.core.third.magic.constant.PosterMergeConstants;
import com.coocaa.ops.plus.core.third.magic.model.*;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * @author bjh
 * @description
 * @date 2021/1/6 19:06
 */
@UtilityClass
public class AiPosterMergeHandler {

    public static PosterMergeParamsInfo getPosterMergeParamsInfo(OriginMergeInfo originMergeInfo) {
        PosterMergeParamsInfo mergeParamsInfo = new PosterMergeParamsInfo();
        mergeParamsInfo.setPosterUrl(originMergeInfo.getPosterUrl());
        mergeParamsInfo.setBizType("homepage/ape");
        mergeParamsInfo.setDrawCover(1);
        mergeParamsInfo.setOutFormat("jpg");
        mergeParamsInfo.setDrawCircleCorner(0);
        mergeParamsInfo.setBlockPositionInfo(originMergeInfo.getBlockPositionInfo());
        List<DrawItemInfo> drawItemInfoList = getAllDrawItemInfos(originMergeInfo);
        mergeParamsInfo.setDrawList(drawItemInfoList);
        return mergeParamsInfo;
    }

    private static List<DrawItemInfo> getAllDrawItemInfos(OriginMergeInfo originMergeInfo) {
        List<DrawItemInfo> result = InstanceUtil.newArrayList();
        List<DrawItemInfo> iconDrawItemInfos = getDrawItemInfosForIcon(originMergeInfo.getIconTextPositions());
        if (CollectionUtils.isNotEmpty(iconDrawItemInfos)) {
            result.addAll(iconDrawItemInfos);
        }
        DrawItemInfo subTitleDrawItemInfo = getDrawItemInfoForSubTitle(originMergeInfo.getSubtitle());
        if (null != subTitleDrawItemInfo) {
            result.add(subTitleDrawItemInfo);
        }
        DrawItemInfo titleDrawItemInfo = getDrawItemInfoForTitle(originMergeInfo.getTitle(), originMergeInfo.getSubtitle(), originMergeInfo.getBlockPositionInfo());
        if (null != titleDrawItemInfo) {
            result.add(titleDrawItemInfo);
        }
        DrawItemInfo scoreDrawItemInfo = getDrawItemInfoForScore(originMergeInfo.getScore());
        if (null != scoreDrawItemInfo) {
            result.add(scoreDrawItemInfo);
        }
        return result;
    }

    private static List<DrawItemInfo> getDrawItemInfosForIcon(List<AiIconInfoModel> iconTextPositions) {
        if (CollectionUtils.isEmpty(iconTextPositions)) {
            return null;
        }
        List<DrawItemInfo> result = InstanceUtil.newArrayList();
        for (AiIconInfoModel aiIconInfoModel : iconTextPositions) {
            DrawItemInfo drawItemInfo = getDrawItemInfoForIcon(aiIconInfoModel);
            if (null != drawItemInfo) {
                result.add(drawItemInfo);
            }
        }
        return result;
    }

    private static DrawItemInfo getDrawItemInfoForIcon(AiIconInfoModel aiIconInfoModel) {
        if (null == aiIconInfoModel || StringUtils.isBlank(aiIconInfoModel.getIconText()) ||
                StringUtils.isBlank(aiIconInfoModel.getPosition())) {
            return null;
        }
        DrawItemInfo drawItemInfo = new DrawItemInfo();
        drawItemInfo.setType("text");
        drawItemInfo.setContent(aiIconInfoModel.getIconText());
        drawItemInfo.setPosition(aiIconInfoModel.getPosition());
        drawItemInfo.setTextSize(24);
        drawItemInfo.setUseAiTextBgColor(1);
        drawItemInfo.setFontWeight("regular");
        return drawItemInfo;
    }

    private static DrawItemInfo getDrawItemInfoForTitle(String title, String subTitle, BlockPositionInfo blockPositionInfo) {
        if (StringUtils.isBlank(title)) {
            return null;
        }
        DrawItemInfo drawItemInfo = new DrawItemInfo();
        drawItemInfo.setType("text");
        drawItemInfo.setContent(title);
        drawItemInfo.setTextSize(PosterMergeConstants.getTitleTextSize(blockPositionInfo.getWidth()));
        drawItemInfo.setFontWeight("medium");
        if (StringUtils.isNotBlank(subTitle)) {
            drawItemInfo.setPosition("left_top");
            drawItemInfo.setMargin(PosterMergeConstants.getTitleSubTitleGap(blockPositionInfo.getWidth()));
            drawItemInfo.setRelativeId("2");
        } else {
            drawItemInfo.setPosition("left_bottom");
        }
        drawItemInfo.setUseTextMark(1);
        drawItemInfo.setOpacity(PosterMergeConstants.OPACITY_TITLE);
        return drawItemInfo;
    }

    private static DrawItemInfo getDrawItemInfoForSubTitle(String subTitle) {
        if (StringUtils.isBlank(subTitle)) {
            return null;
        }
        DrawItemInfo drawItemInfo = new DrawItemInfo();
        drawItemInfo.setType("text");
        drawItemInfo.setContent(subTitle);
        drawItemInfo.setPosition("left_bottom");
        drawItemInfo.setTextSize(28);
        drawItemInfo.setFontWeight("regular");
        drawItemInfo.setId("2");
        drawItemInfo.setUseTextMark(1);
        drawItemInfo.setOpacity(PosterMergeConstants.OPACITY_SUB_TITLE);
        return drawItemInfo;
    }

    private static DrawItemInfo getDrawItemInfoForScore(String score) {
        if (StringUtils.isBlank(score)) {
            return null;
        }
        DrawItemInfo drawItemInfo = new DrawItemInfo();
        drawItemInfo.setType("text");
        drawItemInfo.setContent(score);
        drawItemInfo.setPosition("right_bottom");
        drawItemInfo.setTextSize(28);
        drawItemInfo.setFontWeight("regular");
        drawItemInfo.setMargin(new Integer[]{20, 20});
        drawItemInfo.setUseTextMark(1);
        drawItemInfo.setOpacity(PosterMergeConstants.OPACITY_SUB_TITLE);
        return drawItemInfo;
    }

}
